package com.greenbook.controller.user;

import com.greenbook.result.Result;
import com.greenbook.utils.MinioUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
@RequestMapping("/file")
@Tag(name = "文件相关接口")
@Slf4j
@RequiredArgsConstructor
public class FileController {
    private final MinioUtil minioUtil;
    /**
     * 文件上传
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @Operation(summary = "文件上传")
    public Result<String> upload(MultipartFile file) {
        log.info("文件上传{}", file.getOriginalFilename());
        //获取原始文件名
        String originalFilename = file.getOriginalFilename();
        //获取扩展名
        String extension = originalFilename.substring(originalFilename.lastIndexOf('.'));
        //拼接成上传的文件对象名
        String objectName = UUID.randomUUID() + extension;
        String filePath = minioUtil.upload(file, objectName);

        return Result.success(filePath);
    }
}
